////////////////////////////////////////////////////////////////////////////////
//  COPYRIGHT (C) 2010 TELECOM DIGITAL MEDIA ("TDMEDIA").
//  ALL RIGHTS RESERVED.
//
//  THIS IS CONFIDENTIAL AND PROPRIETARY INTELLECTUAL PROPERTY OWNED BY AND
//  CREATED ON BEHALF OF TDMEDIA. ANY FORM OF DISTRIBUTION, COPY,
//  MODIFICATION WITHOUT THE WRITTEN CONSENT FROM TDMEDIA IS STRICTLY
//  PROHIBITED.
////////////////////////////////////////////////////////////////////////////////
package cc.telecomdigital.tdstock.activity.groups.stock;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import cc.telecomdigital.tdstock.CommonDefn;
import cc.telecomdigital.tdstock.ITDLApplication;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.Http.H_CODE;
import cc.telecomdigital.tdstock.Http.HttpSuperClassStockBaseActivity;
import cc.telecomdigital.tdstock.Http.Services.HttpAppInfo;
import cc.telecomdigital.tdstock.Http.Services.H_HttpServiceRequest.H_UserClient_HttpRequest_Result;
import cc.telecomdigital.tdstock.Tools.ActivityGroupController;
import cc.telecomdigital.tdstock.activity.groups.StockGroup;
import cc.telecomdigital.tdstock.adapter.TwoTextColumnHolder;
import cc.telecomdigital.tdstock.adapter.TwoTextColumnItemAdapter;

/**
 * @author haibo for list item named "AH Comparison"
 */
public class SearchWarrantActivity extends HttpSuperClassStockBaseActivity
{
	protected static final String		TAG								= SearchWarrantActivity.class.getSimpleName();
	private static final long			START_ID						= 90000l;

	private ListView					_listView;
	private Button						_toolBtnReturn;
	private TwoTextColumnItemAdapter	_adapter;
	private List<TwoTextColumnHolder>	_list;
	private SharedPreferences			_prefsStockCode;

	// by ouyangzhihui 2011-06-15
	public static boolean				isNextActivityStockInfo			= false;
	public static boolean				isFromActivityStockInfo			= false;
	public static boolean				isFromActivityBackToWarrantCBBC	= false;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_warrant);

		_toolBtnReturn = (Button) findViewById(R.id.more_returnbtn);
		_toolBtnReturn.setFocusable(true);
		_toolBtnReturn.setClickable(true);
		_toolBtnReturn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				TDStockLog.d(TAG, "close current activity and forwarding to the previous");

				if (isFromActivityStockInfo)
					isFromActivityBackToWarrantCBBC = true; // by ouyangzhihui
															// 2011-6-15

				Intent intent = new Intent(getApplicationContext(), WarrantCBBCActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NO_USER_ACTION);

				SwitchForwardActivity(WarrantCBBCActivity.class, intent);
			}
		});

		_listView = (ListView) findViewById(R.id.search_warrant_list);

		_list = new ArrayList<TwoTextColumnHolder>();

		_adapter = new TwoTextColumnItemAdapter(LayoutInflater.from(this), _list);

		// add a link to CurrentStockInfoActivity
		_listView.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id)
			{
				// the first line is the title
				if (position != -1 && _list.size() >= position + 1)
				{
					try
					{
						String stockCode = String.valueOf(Integer
								.parseInt(_list.get(position).left
										.getText().toString()));
						_prefsStockCode = getSharedPreferences(
								CommonDefn.SYSTEM_CURRENT_SETTING,
								Context.MODE_PRIVATE);
						_prefsStockCode.edit().putString(
								CommonDefn.PREFERENCE_STOCK_CODE,
								stockCode).commit();

						isNextActivityStockInfo = true; // by ouyangzhihui
														// 2011-06-15
						// BMP Mode by ouyangzhihui 2011-10-13
						Class<? extends Activity> _cls = CurrentStockInfoActivity.class;
						if (((ITDLApplication) getApplicationContext()).getIsBMPMode())
						{
							_cls = BMPCurrentStockInfoActivity.class;
						}
						Intent intent = new Intent(getApplicationContext(), _cls);
						intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NO_USER_ACTION);

						SwitchForwardActivity(_cls, intent);
					}
					catch (NumberFormatException e)
					{
						e.printStackTrace();
					}
				}
			}
		});

		_listView.setAdapter(_adapter);
	}

	@Override
	protected void onPause()
	{
		super.onPause();
		// by ouyangzhihui 2011-06-15
		isFromActivityStockInfo = false;
	}

	@Override
	protected void onResume()
	{
		if (isFromActivityStockInfo)
		{
			SetExectuHttpRequestOnResume(false);
			// return; // by ouyangzhihui 2011-06-15
		}
		else
		{
			SetExectuHttpRequestOnResume(true);
			_toolBtnReturn.setText(WarrantCBBCActivity.currStockName);
		}

		super.onResume();
		_listView.invalidateViews();
	}

	@SuppressWarnings("unchecked")
	private void updateAdapter(List<String[]> storeList)
	{
		if (storeList.size() <= 0)
		{ // mock the empty row
			_list.clear();
			for (int index = 0; index < 20; index++)
			{
				TwoTextColumnHolder holder = new TwoTextColumnHolder();
				holder.left = new TextView(this);
				holder.right = new TextView(this);
				holder.id = index;
				_list.add(holder);
			}
			_adapter.notifyDataSetChanged();
			return;
		}
		_list.clear();
		for (String[] array : storeList)
		{
			TwoTextColumnHolder holder = new TwoTextColumnHolder();
			holder.left = new TextView(this);
			holder.right = new TextView(this);
			holder.id = START_ID + _list.size();
			holder.left.setText(array[0] == null ? CommonDefn.DEFAULT_FIELD_VALUE : array[0]);
			holder.right.setText(array[1] == null ? CommonDefn.DEFAULT_FIELD_VALUE : array[1]);
			_list.add(holder);
		}
		// ((ITDLApplication) getApplicationContext()).setObject(null);
		_adapter.notifyDataSetChanged();
	}

	@Override
	public ActivityGroupController GetActivityGroupController()
	{
		return StockGroup.GetActivityGroupHostControl();
	}

	static public String[]	wParams;

	static public void SetWarrantSearchParams(String[] iWParams)
	{
		wParams = iWParams;
	}

	@Override
	protected H_CODE GetDataFromHttpServer()
	{
		progressView.SetShowDialog(true);
		return HttpAppInfo.SendWarrantSearchRequest(actContext, WarrantCBBCActivity.currStockCode, wParams, this);
	}

	@Override
	protected void H_UserClient_HttpRequest_Response_Sub(final H_UserClient_HttpRequest_Result iResult)
	{
		if (iResult.MStatus.equals("0") == false)
		{
			// Prompt Error
			ShowMessageDialog(iResult.MError + "(" + iResult.MStatus + ")");
			return;
		}
		
		Runnable runnable = new Runnable()
		{
			public void run()
			{
				updateAdapter(iResult.contentStores);
			}
		};
		_messageHandler.post(runnable);
	}

}
